package com.vtb.listener;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vtb.entity.Config;
import com.vtb.servlet.WtbException;

/**
 * 
 * 初始化系统信息
 * @author tacheno
 *
 */
public class ConfigListener implements ServletContextListener {
	private static final Log log = LogFactory.getLog(ConfigListener.class);
	public void contextDestroyed(ServletContextEvent sce) {

	}
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext context = sce.getServletContext();
		Properties prop = new Properties();
		InputStream inputStream = null;
		try {
		inputStream = context
				.getResourceAsStream("WEB-INF/classes/config.properties");
			prop.load(inputStream);
			Config.setQQ_FROM(prop.getProperty("qq_from").split(","));
			Config.setRR_FROM(prop.getProperty("rr_from").split(","));
			Config.setMAIL_SERVER_HOST(prop.getProperty("mail_server_host"));
			Config.setMAIL_USER_NAME(prop.getProperty("mail_user_name"));
			Config.setMAIL_PASSWORD(prop.getProperty("mail_password"));
			Config.setMAIL_FROM(prop.getProperty("mail_from"));
			String receivers = (String) prop.get("mail_receivers");
			if (receivers != null && !receivers.equals("")) {
				Config.setMAIL_RECEIVERS(receivers.split(","));
			}
			log.info("系统配置文件初始化成功！！");
		} catch (IOException e) {
			log.error("系统配置文件初始化失败：", e);
			throw new WtbException("系统配置文件初始化失败",e);
		}finally{
			try {
				inputStream.close();
			} catch (IOException e) {
				log.error("数据流关闭失败：", e);
			}
		}

	}
}
